package cn.drew.house.controller;

import cn.drew.basic.utils.AjaxResult;
import cn.drew.basic.utils.PageList;
import cn.drew.house.domain.ContractInformation;
import cn.drew.house.query.ContractInformationQuery;
import cn.drew.house.service.ContractInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contract")
public class ContractInformationController {

    @Autowired
    private ContractInformationService contractInformationService;

    @GetMapping
    public List<ContractInformation> findALl(){
        List<ContractInformation> list = contractInformationService.findAll();
        return list;
    }

    @GetMapping("/findByHouseid/{houseId}")
    public ContractInformation findByHouseid(@PathVariable("houseId") Integer houseId){
        System.out.println(houseId);
        Integer houseId1 = Integer.valueOf(houseId);
        ContractInformation contractInformation =  contractInformationService.findByHouseid(houseId1);
        System.out.println(contractInformation);
        return contractInformation;
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ContractInformation contractInformation){
        if(contractInformation.getCtId() != null){
            contractInformationService.update(contractInformation);
        }else{
            contractInformationService.add(contractInformation);
        }

        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult Delete(@PathVariable("id") Long id){
        contractInformationService.delete(id);
        System.out.println(id);
        return AjaxResult.success();
    }

//    分页查询
    @PostMapping
    public PageList<ContractInformation> queryByPage(@RequestBody ContractInformationQuery contractInformationQuery){
        System.out.println(contractInformationQuery);
        PageList<ContractInformation> pageList = contractInformationService.queryByPage(contractInformationQuery);
        return pageList;
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        contractInformationService.patchDelete(ids);
        return AjaxResult.success();
    }
    
//    //无线级数
//    @GetMapping("/tree")
//    public List<Department> tree(){
//        List<Department> tree = contractInformationService.tree();
//        System.out.println(tree);
//        return contractInformationService.tree();
//
//    }


    
}
